package com.ruoyi.manage.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.manage.domain.SurgeryDeviceIncreasement;
import com.ruoyi.manage.mapper.SurgeryDeviceDetailMapper;
import com.ruoyi.manage.domain.SurgeryDeviceDetail;
import com.ruoyi.manage.service.ISurgeryDeviceDetailService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 术中清点记录Service业务层处理
 * 
 * @author zww
 * @date 2025-07-08
 */
@Service
public class SurgeryDeviceDetailServiceImpl implements ISurgeryDeviceDetailService 
{
    @Autowired
    private SurgeryDeviceDetailMapper surgeryDeviceDetailMapper;

    /**
     * 查询术中清点记录
     * 
     * @param surgeryDeviceDetailId 术中清点记录主键
     * @return 术中清点记录
     */
    @Override
    public SurgeryDeviceDetail selectSurgeryDeviceDetailBySurgeryDeviceDetailId(Long surgeryDeviceDetailId)
    {
        return surgeryDeviceDetailMapper.selectSurgeryDeviceDetailBySurgeryDeviceDetailId(surgeryDeviceDetailId);
    }

    /**
     * 查询术中清点记录列表
     * 
     * @param surgeryDeviceDetail 术中清点记录
     * @return 术中清点记录
     */
    @Override
    public List<SurgeryDeviceDetail> selectSurgeryDeviceDetailList(SurgeryDeviceDetail surgeryDeviceDetail)
    {
        // 只查询一次列表
        List<SurgeryDeviceDetail> surgeryDeviceDetailList = surgeryDeviceDetailMapper.selectSurgeryDeviceDetailList(surgeryDeviceDetail);
        for (SurgeryDeviceDetail detail : surgeryDeviceDetailList) {
            detail.setSurgeryDeviceIncreasementList(selectSurgeryDeviceIncreasementList(detail.getSurgeryDeviceDetailId()));
        }
        return surgeryDeviceDetailList;
    }
    public List<SurgeryDeviceDetail> selectSurgeryDeviceDetailList(Long surgeryDeviceId)
    {
        // 只查询一次列表
        List<SurgeryDeviceDetail> surgeryDeviceDetailList = surgeryDeviceDetailMapper.selectSurgeryDeviceDetailListBySurgeryDeviceId(surgeryDeviceId);
        for (SurgeryDeviceDetail detail : surgeryDeviceDetailList) {
            detail.setSurgeryDeviceIncreasementList(selectSurgeryDeviceIncreasementList(detail.getSurgeryDeviceDetailId()));
        }
        return surgeryDeviceDetailList;
    }

    public List<SurgeryDeviceIncreasement> selectSurgeryDeviceIncreasementList(Long surgeryDeviceDetailId){
        return surgeryDeviceDetailMapper.selectSurgeryDeviceIncreasementList(surgeryDeviceDetailId);
    }

    /**
     * 新增术中清点记录
     * 
     * @param surgeryDeviceDetail 术中清点记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSurgeryDeviceDetail(SurgeryDeviceDetail surgeryDeviceDetail)
    {
        setCreateInfo(surgeryDeviceDetail);
        int rows = surgeryDeviceDetailMapper.insertSurgeryDeviceDetail(surgeryDeviceDetail);
        insertSurgeryDeviceIncreasement(surgeryDeviceDetail);
        return rows;
    }

    @Transactional
    @Override
    public int insertSurgeryDeviceDetail(List<SurgeryDeviceDetail> surgeryDeviceDetailList)
    {
        int totalRows = 0;
        for (SurgeryDeviceDetail detail : surgeryDeviceDetailList) {
            if (detail.getSurgeryDeviceDetailId() == null) {
                setCreateInfo(detail);
                totalRows += surgeryDeviceDetailMapper.insertSurgeryDeviceDetail(detail);
            }
            else {
                setUpdateInfo(detail);
                totalRows += surgeryDeviceDetailMapper.updateSurgeryDeviceDetail(detail);
            }
            insertSurgeryDeviceIncreasement(detail);
        }
        return totalRows;
    }

    /**
     * 修改术中清点记录
     * 
     * @param surgeryDeviceDetail 术中清点记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSurgeryDeviceDetail(SurgeryDeviceDetail surgeryDeviceDetail)
    {
        setUpdateInfo(surgeryDeviceDetail);
        surgeryDeviceDetailMapper.deleteSurgeryDeviceIncreasementBySurgeryDeviceDetailId(surgeryDeviceDetail.getSurgeryDeviceDetailId());
        insertSurgeryDeviceIncreasement(surgeryDeviceDetail);
        return surgeryDeviceDetailMapper.updateSurgeryDeviceDetail(surgeryDeviceDetail);
    }

    /**
     * 批量删除术中清点记录
     * 
     * @param surgeryDeviceDetailIds 需要删除的术中清点记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSurgeryDeviceDetailBySurgeryDeviceDetailIds(Long[] surgeryDeviceDetailIds)
    {
        surgeryDeviceDetailMapper.deleteSurgeryDeviceIncreasementBySurgeryDeviceDetailIds(surgeryDeviceDetailIds);
        return surgeryDeviceDetailMapper.deleteSurgeryDeviceDetailBySurgeryDeviceDetailIds(surgeryDeviceDetailIds);
    }

    /**
     * 删除术中清点记录信息
     * 
     * @param surgeryDeviceDetailId 术中清点记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSurgeryDeviceDetailBySurgeryDeviceDetailId(Long surgeryDeviceDetailId)
    {
        surgeryDeviceDetailMapper.deleteSurgeryDeviceIncreasementBySurgeryDeviceDetailId(surgeryDeviceDetailId);
        return surgeryDeviceDetailMapper.deleteSurgeryDeviceDetailBySurgeryDeviceDetailId(surgeryDeviceDetailId);
    }

    /**
     * 新增术中清点单记录增加明细信息
     */
    public void insertSurgeryDeviceIncreasement(SurgeryDeviceDetail surgeryDeviceDetail)
    {
        List<SurgeryDeviceIncreasement> list = surgeryDeviceDetail.getSurgeryDeviceIncreasementList();
        if (StringUtils.isNotEmpty(list)) {
            Long detailId = surgeryDeviceDetail.getSurgeryDeviceDetailId();
            // 使用迭代器安全地移除元素
            java.util.Iterator<SurgeryDeviceIncreasement> iterator = list.iterator();
            while (iterator.hasNext()) {
                SurgeryDeviceIncreasement increasement = iterator.next();
                if (increasement.getSurgeryDeviceIncreasementId() != null) {
                    // 使用迭代器的 remove 方法移除元素
                    iterator.remove();
                } else {
                    increasement.setSurgeryDeviceDetailId(detailId);
                }
            }
            // 使用 !list.isEmpty() 替代 list.size() > 0
            if (!list.isEmpty()) {
                surgeryDeviceDetailMapper.batchSurgeryDeviceIncreasement(list);
            }
        }
    }

    /**
     * 设置创建信息
     * @param detail 术中清点记录
     */
    private void setCreateInfo(SurgeryDeviceDetail detail) {
        detail.setCreateBy(getUsername());
        detail.setUpdateBy(getUsername());
    }

    /**
     * 设置更新信息
     * @param detail 术中清点记录
     */
    private void setUpdateInfo(SurgeryDeviceDetail detail) {
        detail.setUpdateBy(getUsername());
    }
}
